﻿namespace PowerSong.Export
    {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    using PowerSong.SongDatabase;

    public class OpenLyricsExport : BaseExport
        {

        private string targetFolderLocation = null;

        private Song[] songs = null;

        public OpenLyricsExport(Database sourceDatabase, Song[] songs, string targetFolderLocation)
            : base(sourceDatabase)
            {
            this.targetFolderLocation = targetFolderLocation;
            this.songs = songs;
            }

        protected override void Export(List<string> messages)
            {

            const string UnencodedNewLineTag = "<br />";
            const string EncodedNewLineTag = "&lt;br /&gt;";

            // Create the folder if necessary
            if (!Directory.Exists(targetFolderLocation)) Directory.CreateDirectory(targetFolderLocation);

            // Perform the export
            foreach (var song in songs)
                {

                // Compose the document
                XNamespace ns = "http://openlyrics.info/namespace/2009/song";
                var document = new XDocument(
                    new XElement(ns + "song",
                        new XAttribute("version", "0.8"),
                        new XAttribute("createdIn", "PowerSong 2.0"),
                        new XElement(ns + "properties",
                            new XElement(ns + "titles",
                                new XElement(ns + "title", song.Title)),
                            new XElement(ns + "authors",
                                from string author in song.Authors orderby author select new XElement(ns + "author", author)),
                            new XElement(ns + "copyright", song.Copyright),
                            new XElement(ns + "comments",
                                from Category category in song.Categories select new XElement(ns + "comment", "Song was placed in category '" + category.Name + "'."))),
                        new XElement(ns + "lyrics",
                            from int verseIndex
                            in Enumerable.Range(0, song.Verses.Count)
                            select new XElement(ns + "verse",
                                new XAttribute("name", string.Format("v{0}", verseIndex + 1)),
                                new XElement(ns + "lines", song.Verses[verseIndex].Replace(Environment.NewLine, UnencodedNewLineTag))
                            )
                        )
                    )
                );                

                // Ensure the newline tags are encoded correctly, because the target files aren't "real" xml files
                var text = document.ToString();
                text = text.Replace(EncodedNewLineTag, "<br />");

                // Create a good file name for the file
                var fileName = GetSafeFileName(song.Title);
                var filePath = Path.Combine(targetFolderLocation, fileName + ".xml");
                if (File.Exists(filePath)) filePath = Path.Combine(targetFolderLocation, song.ID.ToString() + ".xml");

                // Save the file
                File.WriteAllText(filePath, text);

                }

            }

        }

    }
